//
//  GoodListCtr.swift
//  soapp
//
//  Created by zhu xietong on 2017/8/16.
//  Copyright © 2017年 CocoaPods. All rights reserved.
//

import UIKit
import Eelay
import soapp
import Alamofire


public class ThemeSet {
   public struct Gradient{
        public static var buy:[UIColor] = [UIColor(shex:"#EE5354"),UIColor(shex:"#BD1415")]
        public static var cart:[UIColor] = [UIColor(shex:"#A5A5A5"),UIColor(shex:"727272")]
        public static var groupBuy:[UIColor] = [UIColor(shex:"#EE5354"),UIColor(shex:"#BD1415")]
        public static var groupSingle:[UIColor] = [UIColor(shex:"#A5A5A5"),UIColor(shex:"727272")]
        public static var save:[UIColor] = [UIColor(shex:"#A5A5A5"),UIColor(shex:"727272")]
    
        public static var disable:[UIColor] = [UIColor(shex:"#888"),UIColor(shex:"#444")]
    
    }
    public static var buttonSpace:CGFloat = 6
    public static var buttonRadius:CGFloat = 2
}



public struct Goods {
    

    

    
    public static var DetailCtrType = GoodsDetailCtr.self
    public static var SellerCtrType = TypeInitController.self
    public static var GoodsListCtrType = GoodsListCtr.self
    public static var OrderCtrType = ShoppingOrderDetailCtr.self
    public static var SearchCtrType = GoodsSearchCtr.self
    public static var supportGlobal = true
    

    
//    public static var supportSaleAfter = true//是否开启售后


    

    
    public static var listInset = UIEdgeInsets.zero
    
    public static var minimumInteritemSpacing:CGFloat = 0
    public static var minimumLineSpacing:CGFloat = 0

    
    public static func seeDetail(obj:NSMutableDictionary,at:UIViewController?){
        let ctr = DetailCtrType.init()
        ctr.mo.model = obj
        at?.navigationController?.pushViewController(ctr, animated: true)
    }
    
    public static func seeOneCate(obj:NSMutableDictionary,at:UIViewController?){
        let ctr = GoodsListCtr()
        ctr.listMode = .cate(name: obj["name",""], ID: obj["id",""])
        at?.navigationController?.pushViewController(ctr, animated: true)
    }
    
    public static func seeComments(goods:NSMutableDictionary,at:UIViewController?){
        let ctr = GoodsCommentListCtr()
        
        let scheme = SaleScheme(rawValue: goods["__scheme",""]) ?? .normal
        ctr.scheme = scheme
        ctr.mo.model = goods
        at?.navigationController?.pushViewController(ctr, animated: true)
    }
    
    public static func seeSeller(obj:NSMutableDictionary,at:UIViewController?){
        let ctr = SellerCtrType.init()
        ctr.mo.model = obj
        at?.navigationController?.pushViewController(ctr, animated: true)
    }
    
    public static func seeGoods(filters:[String:Any],at:UIViewController?){
        let ctr = GoodsListCtrType.init()
        ctr.listMode = .link(params: filters)
        at?.navigationController?.pushViewController(ctr, animated: true)
    }
    
    
    public static func seeOrder(obj:NSMutableDictionary,at:UIViewController?){
        let ctr = OrderCtrType.init()
        ctr.mo.model = obj
        at?.navigationController?.pushViewController(ctr, animated: true)
    }
    
    
    public static let space:CGFloat = 6
    public static let kit_width = (Swidth-Goods.space*2.0-Goods.space)/2.0
    public static let row_height = Goods.kit_width + 60 + Goods.space
    
    

    
    public class OneGoodsCell: RuleCell {
        let imgV = UIImageView()
        let titleL = UILabel()
        let priceL = UILabel()
        
        override public func addLayoutRules() {
            contentView.addSubviews(imgV,titleL,priceL)
            imgV.frame = [0,0,kit_width,kit_width]
            titleL.frame = [0,(kit_width + 4),(kit_width - 2),34]
            priceL.frame = [0,(kit_width + 40),(kit_width/2.0-6),14]
            
            //            contentView.eelay = [
            //                [imgV,[ee.T.L.R]],
            //                [titleL,[imgV,ee.B,ee.T,6],[ee.B,-6],[ee.L]]
            //            ]
            //            imgV.addConstraint(NSLayoutConstraint(item: imgV, attribute: .height, relatedBy: .equal, toItem: imgV, attribute: .width, multiplier: 0.9, constant: 0))
            
            titleL.ui.font10.cl_222.left.line(2)
            priceL.ui.right.font11.text(hex: UIColor.main_hex)
            imgV.ui.holder(type: .grayBack)
            backgroundColor = .white
            
            //        let tap = UITapGestureRecognizer(target: self, action: #selector(open))
            //        imgV.addGestureRecognizer(tap)
        }
        
        override public func mo_model_reload() {
            
            imgV.img_url = mo.model["thumb",""]
            titleL.text = mo.model["goods_title",""]
            priceL.text = "¥ " + mo.model["shop_price","100"]
            
        }
    }
    
    class TowGoodsCel: JoTableCell {
        let listV = RuleView<OneGoodsCell>.init()
        override func addLayoutRules() {
            //            contentView.eelay = [
            //                [listV,[ee.T.L.B.R]]
            //            ]
            contentView.addSubviews(listV)
            listV.space = (Goods.space,Goods.space)
            listV.limit_width = Swidth
            listV.padding = [Goods.space/2.0,Goods.space,Goods.space/2.0,Goods.space]
            listV.que_num = 2
            listV.item_height = Goods.row_height - Goods.space
            
            backgroundColor = .clear
            contentView.backgroundColor = .clear
            listV.backgroundColor = .clear
            
            weak var wself = self
            listV.touchAction = { _,_,data in
                let ctr = GoodsDetailCtr()
                ctr.mo.model = data
                wself?.mo.controller?.navigationController?.pushViewController(ctr, animated: true)
            }
        }
        //    func active(onwer: Any, actionID: String, data: NSMutableDictionary) {
        //        let ctr = GoodsDetailCtr()
        //        ctr.mo.model = data
        //        content_controller?.navigationController?.pushViewController(ctr, animated: true)
        //    }
        
        override func mo_model_reload() {
            listV.mo.model = mo.model
        }
    }
    
    
    
    
    
    
    public class OneJFGoodsCell: RuleCell {
        let imgV = UIImageView()
        let titleL = UILabel()
        let priceL = UILabel()
        
        override public func addLayoutRules() {
            contentView.addSubviews(imgV,titleL,priceL)
            imgV.frame = [0,0,kit_width,(kit_width*0.9)]
            titleL.frame = [0,(kit_width*0.9 + 8.0),(kit_width/2.0),20]
            priceL.frame = [(kit_width/2.0+6),(kit_width*0.9 + 8.0),(kit_width/2.0-6),20]
            
            //            contentView.eelay = [
            //                [imgV,[ee.T.L.R]],
            //                [titleL,[imgV,ee.B,ee.T,6],[ee.B,-6],[ee.L]]
            //            ]
            //            imgV.addConstraint(NSLayoutConstraint(item: imgV, attribute: .height, relatedBy: .equal, toItem: imgV, attribute: .width, multiplier: 0.9, constant: 0))
            
            titleL.ui.font12.cl_222.left.line(2)
            priceL.ui.right.font12.text(hex: UIColor.main_hex)
            imgV.ui.holder(type: .image(.goods_holder)).set(radius: 0, width: 0.4, color: UIColor(shex: AppTheme.border_hex))
            //        let tap = UITapGestureRecognizer(target: self, action: #selector(open))
            //        imgV.addGestureRecognizer(tap)
        }
        
        override public func mo_model_reload() {
            
            imgV.img_url = mo.model["thumb",""]
            titleL.text = mo.model["title",""]
            let price = mo.model[float:"credit",0]
            priceL.attributedText = "\(price.print(num: 0))".ui.bfont14.right.color(hex: UIColor.main_hex).astring + "\("积分".loc)".ui.font12.right.color(hex: UIColor.main_hex).astring
            
        }
    }
    
    
    class TowJFGoodsCel: JoTableCell {
        let listV = RuleView<OneJFGoodsCell>.init()
        override func addLayoutRules() {
            //            contentView.eelay = [
            //                [listV,[ee.T.L.B.R]]
            //            ]
            contentView.addSubviews(listV)
            listV.space = (18,10)
            listV.limit_width = Swidth
            listV.padding = [10,AppTheme.edge,10,AppTheme.edge]
            listV.que_num = 2
            listV.item_height = (kit_width * 0.9) + 36
            
//            weak var wself = self
            listV.touchAction = { _,_,data in
                //                let ctr = 积分GoodsDetailCtr()
                //                ctr.mo.model = data
                //                wself?.content_controller?.navigationController?.pushViewController(ctr, animated: true)
            }
        }
        //    func active(onwer: Any, actionID: String, data: NSMutableDictionary) {
        //        let ctr = GoodsDetailCtr()
        //        ctr.mo.model = data
        //        content_controller?.navigationController?.pushViewController(ctr, animated: true)
        //    }
        
        override func mo_model_reload() {
            listV.mo.model = mo.model
        }
    }
    
    
    
    
    
    
    
    
}


